﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLDC.Common.Estate;
using System.Data;

namespace ZLDC.Estate.Quantify
{
    public partial class lp_l_scores_set : System.Web.UI.Page
    {

        private static int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetDeptFraction();
                int deptId = 0;
                lblDeptName.Text = _Utility.GetInfoBySession().m_sDeptName;//部门名称
                deptId = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID,0);//部门ID

                if (_Utility.GetInfoBySession().m_sRoleID == "1") ///系统管理员
                {
                    lblDeptName.Text = _Utility.GetWebConfigSetting("deptName").ToString();
                    deptId =_Convert.ToInt(_Utility.GetWebConfigSetting("deptId").ToString());
                }

                ///判断是否已经存在部门最低分数设置
                DataSet ds = ZLDC.Topo.BLL.Estate.lp_l_scores_set.GetDS(deptId);
                
                if (ds.Tables[0].Rows.Count > 0)
                {
                    id = _Convert.ToInt(ds.Tables[0].Rows[0]["id"].ToString(), 0);

                    Session["id"] = id;
                   
                    lowsetScores.Text = ds.Tables[0].Rows[0]["itemvalue"].ToString();//最低分数
                }
            }
        }


        public string Fractionlist="";
        protected void GetDeptFraction()
        {
            string sql = "select * from t_system_option where item='每日量化最低分数' "+
                         "and subsidiaryID in (select * from f_GetAllFatherDeptAll(" + _Utility.GetInfoBySession().m_sDeptID + ")) and subsidiaryID!=" + _Utility.GetInfoBySession().m_sDeptID + " and subsidiary!='' order by grade desc";
            DataSet ds=ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sql);
            if(ZLDC.Topo.BLL.Estate.lp_Basis.rtcount(ds))
            {
                string text = "不得低于（上级） ：";
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    text += ds.Tables[0].Rows[i]["subsidiary"] + ":<span style='color:red'>" + ds.Tables[0].Rows[i]["itemValue"].ToString() + "</span>(分)   ";
                    Fractionlist += ds.Tables[0].Rows[i]["itemValue"].ToString() + ",";
                }
                spandept.InnerHtml = text;
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            string deptName = "";
            int deptId = 0;
            int grade = -1;
            if (_Utility.GetInfoBySession().m_sRoleID == "1") ///系统管理员，默认设分公司最低分数
            {
                deptName =_Utility.GetCompanyName();//分公司
                switch (deptName)
                {
                    case "深圳":
                        deptId = 926;
                        break;
                    case "长沙":
                        deptId = 1250;
                        break;
                    case "武汉":
                        deptId = 2513;
                        break;
                    case "广州":
                        deptId = 803;
                        break;
                    
                }
                
                grade = 0;
            }
            else
            {
                deptName = _Utility.GetInfoBySession().m_sDeptName;
                deptId = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID);//分公司ID
            }

            string typeName = "";//部门类型
            DataTable dt = ZLDC.DAL.Estate.DeptDAL.getDeptInfo(deptId);

            if (dt != null && dt.Rows.Count > 0)
            {
                typeName = dt.Rows[0]["type"].ToString();
            }

            int typeid = 4;
            string item = "每日量化最低分数";
            int itemvale = _Convert.ToInt(lowsetScores.Text.Trim());

            switch (typeName)
            {
                case "营业大部":
                    grade = 1;
                    break;

                case "营业部":
                    grade = 2;
                    break;

                case "区域":
                    grade = 3;
                    break;
                case "分行":
                    grade = 4;
                    break;

            }

            if (Session["id"] != null) 
            {
                id =_Convert.ToInt(Session["id"].ToString(),0);
            }

            string outmsg = ZLDC.Topo.BLL.Estate.SystemOptionBLL.p_system_option_save(id, deptId, deptName, typeid, item, itemvale, "", "", 0, _Utility.GetInfoBySession().m_sUserName, "", grade);

            _Utility.GoError(outmsg);
        }
    }
}
